package models

import (
	"chatgpt/helper"
	"chatgpt/logq"
	"chatgpt/plugin/elastic"
	"github.com/slclub/go-tips"
)

type Search struct {
	SystemWord string
}

// @return string prompt
// @return bool  是否使用了 索引
func (s *Search) CustomQuestion(prompt string) (string, bool) {

	result, bt := s.CustomElasticSearch(prompt)
	if bt == false {
		return prompt, bt
	}

	return "system:" + s.SystemWord + "\n\n" + result + "\n\nuser:" + prompt, true
}

//
//func customFoundSearch(prompt string) (string, bool) {
//	data := found.Data{}
//	err := found.Default.Query(&data, prompt, map[string]any{})
//	if err != nil || len(data.Documents) == 0 {
//		return prompt, false
//	}
//	result := ""
//	for _, item := range data.Documents {
//		result += item.Document.Prompt + "\n" + item.Document.Completion + "\n\n"
//	}
//	result = result[:len(result)-2]
//	return "system:" + GLM_PROMPT_PRIFIX + "\n\n" + result + "\n\nuser:" + prompt, true
//}

func (s *Search) CustomElasticSearch(prompt string) (string, bool) {
	cnf := helper.Config().Sub("elastic")
	if !cnf.GetBool("open") {
		return prompt, false
	}
	dataarr := elastic.Client.Search(prompt, "prompt")
	logq.InfoF("ES.GET lenght:%v prompt%v", len(dataarr), prompt)
	dataarr = elastic.Client.Filter(dataarr)
	dataarr = elastic.Client.Bigger(dataarr)
	if len(dataarr) == 0 {
		return prompt, false
	}
	result := ""
	for _, item := range dataarr {
		result += tips.String(item["prompt"]) + "\n" + tips.String(item["content"]) + "\n\n"
	}
	result = result[:len(result)-2]
	return result, true
	// return "system:" + GLM_PROMPT_PRIFIX + "\n\n" + result + "\n\nuser:" + prompt, true
}
